package com.diy.common.bussiness.controller;

import com.diy.common.exception.ProgramException;
import com.diy.common.baseModel.DiyResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by GY on 2015/11/22.
 */
public  class BaseController {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(BaseController.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public DiyResponse handleException(Exception e, HttpServletRequest request,HttpServletResponse response) {

        DiyResponse bizResult=new DiyResponse();

        if (e instanceof ProgramException)
        {
            ProgramException programException=(ProgramException) e;

            bizResult.failure(String.valueOf((programException).getCode()));
            LOGGER.error("program-msg:[{}],code:[{}]",programException.getMsg(),programException.getCode());
        }

        return bizResult;
    }
}
